#include <allegro.h>
#include <stdio.h>
#include <apeg.h>
#include <math.h>
#include "stage2.h"




int main() {

	char chemin_background[]="../images/Map1.bmp";
	char chemin_background_col[]="../images/BACKGROUNDCol.bmp";
	char chemin_music[]="../music/son.wav";
	char chemin_musicc[]="../music/wolf.wav";
	
	BITMAP *Buffer;
	BITMAP *Background=NULL;
	BITMAP *BackgroundCol=NULL;
        BITMAP *BackgroundC=NULL;
        BITMAP *BackgroundS=NULL;
        BITMAP *BackgroundO=NULL;
	BITMAP *BackgroundQ=NULL;
	SAMPLE *music,*wolf;
	Sandra sandra;
	Scroller scrol;
	Arc arc;
	int compteur,b=0/*pause*/,c=0/*compteur pause*/,key_down=0/*controle clavier*/;

	int pix1,pix2,pix3,pix4;
	
	/* General initialization function */
	allegro_init();
	if (install_keyboard() == -1) {
		allegro_message("Error! %s", allegro_error);
		return (-1);
	}
	install_sound(DIGI_AUTODETECT,MIDI_NONE,NULL);
	install_timer();
	//INSTALL MOUSE
	install_mouse() ;
	if(install_mouse() == -1) {
	
	allegro_message("Erreur ! %s", allegro_error) ;
	
	return-1 ;
	}
	set_color_depth(32);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_W, GFX_H, 0, 0) != 0)
	{
	set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* here’s the command for text mode */
	allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
	return (1);
	}

	Buffer = create_bitmap(8800, SCREEN_H);
	
	//camera
	init_scroller(&scrol);

	//loading sandra
	load_sandra(&sandra);

	//loading image arc
	arc.img=load_bitmap("../images/arc.bmp", NULL);
	
	//loading background + background collision
	BackgroundCol=load_background1_bmp(chemin_background_col);
	Background=load_background_bmp(chemin_background);
	
	//Pause        
	BackgroundC= load_bitmap("../images/menu0.bmp", NULL);
	BackgroundS= load_bitmap("../images/menu1.bmp", NULL);
	BackgroundO= load_bitmap("../images/menu2.bmp", NULL);
	BackgroundQ= load_bitmap("../images/menu3.bmp", NULL);
	
	//init position & direction
	compteur = 0;
	init_sandra(&sandra);
	
	//Voulume SOUND 
	set_volume(255, 255);
	//Load music
	music=load_music(chemin_music);
	wolf=load_music(chemin_musicc);

	while (!key[KEY_ESC]) 
	{
	//controle clavier
 	if(key_down == 0 && key[KEY_RIGHT])
 	{
		key_down = 1;
 	}
 	else if(key_down == 0 && key[KEY_UP])
	{
		key_down = 3;
	}
	else if(key_down == 0 && key[KEY_LEFT])
	{
	key_down = 2;
	}
	if (b==0) //pause
	{
	clear_bitmap(Buffer);
	               
	//play music
	play_music(music);
	play_musicc(wolf);
	//drawing background
	draw_background(Background,Buffer,scrol);
		
 	//collision
	pix1=get_pixel(BackgroundCol,sandra.x+ sandra.img_right[0]->w,sandra.y +sandra.img_right[0]->h /2);
	pix2=get_pixel(BackgroundCol,sandra.x,sandra.y + sandra.img_left[0]->h /2);
	pix3=get_pixel(BackgroundCol,sandra.x+ sandra.img_right[0]->w /2,sandra.y);
	pix4=get_pixel(BackgroundCol,sandra.x+ sandra.img_right[0]->w /2,sandra.y + sandra.img_right[0]->h-60);
	
	//move sandra	
	move_sandra(&sandra,&compteur,BackgroundCol,pix1,pix2,pix3,pix4,&arc,&scrol,&key_down);
			
	//drawing sandra
	draw_sandra(sandra,Buffer,compteur,arc);}

	//blit Buffer to screen
        if (key[KEY_P])
	{
	b=1;key_down=4;
	}
        if (b==1)
	{
	b=pause_jeux(BackgroundC,BackgroundS,BackgroundO,BackgroundQ,Buffer,&b,&c);
	if (b==0)
	{key_down=1;}
	}

	blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

	if (compteur == 10000)
	
	compteur = 0;
	}   
	//destroy
	
	destroy_sandra(&sandra);
	destroy_background(Background);
	destroy_bitmap(Buffer);
        destroy_background(BackgroundC);
        destroy_background(BackgroundS);
        destroy_background(BackgroundO);
	destroy_background(BackgroundQ);
	destroy_music(music);
	destroy_music(wolf);
	return (0);
}
	END_OF_MAIN();


